package com.woniuxy.charin.controller;

import com.woniuxy.charin.model.entity.ChargingPile;
import com.woniuxy.charin.model.entity.Station;
import com.woniuxy.charin.model.vo.PageResponse;
import com.woniuxy.charin.model.vo.StationAndChargingPilesRequest;
import com.woniuxy.charin.service.ChargingPileService;
import com.woniuxy.charin.service.StationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/station")
@Slf4j
@Api(tags = "充电站管理")
public class StationController {
    @Autowired
    private StationService stationService;

    @Autowired
    ChargingPileService chargingPileService;

    @GetMapping("/getStationByPage")
    @ApiOperation("分页查询充电站信息")
    public PageResponse<Station> getStationByPage(
            @RequestParam(required = false) String stationName,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String address,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        log.info("getStationByPage: stationName={}, " +
                        "status={}, address={}, page={}, size={}",
                stationName, status, address, page, size);
        return stationService.getStationList(
                stationName, status, address, page, size);
    }

    @GetMapping("/getStationById/{stationId}")
    @ApiOperation("根据id得到充电站信息")
    public  ResponseEntity<StationAndChargingPilesRequest> getStationById(@PathVariable Long stationId) {
        log.info("Fetching station and charging piles for stationId={}", stationId);

        Station station = stationService.getById(stationId);
        if (station == null) {
            return ResponseEntity.notFound().build();
        }

        List<ChargingPile> chargingPiles = chargingPileService.listByStationId(stationId);

        StationAndChargingPilesRequest response = new StationAndChargingPilesRequest();
        response.setStation(station);
        response.setChargingPiles(chargingPiles);

        return ResponseEntity.ok(response);
    }

    @DeleteMapping("/deleteStation")
    @ApiOperation("删除充电站信息")
    public String deleteStation(int id) {
        log.info("deleteStation");
        stationService.deleteStation(id);
        return "删除成功";
    }

    @PostMapping("/addStation")
    @ApiOperation("添加充电站信息")
    public String addStation(@RequestBody Station station) {
        log.info("addStation");
        stationService.addStation(station);
        return "添加成功";
    }

    @PutMapping("/updateStation")
    @ApiOperation("更新充电站信息")
    public String updateStation(Station station) {
        log.info("updateStation");
        stationService.updateStation(station);
        return "更新成功";
    }

    //下线站点
    @PutMapping("/offlineStation/{id}")
    @ApiOperation("下线站点")
    public String offlineStation(@PathVariable int id) {
        log.info("offlineStation");
        stationService.offlineStation(id);
        return "下线成功";
    }

    //上线站点
    @PutMapping("/onlineStation/{id}")
    @ApiOperation("上线站点")
    public String onlineStation(@PathVariable int id) {
        log.info("onlineStation");
        stationService.onlineStation(id);
        return "上线成功";
    }

    //批量删除
    @PostMapping("/batchDelete")
    @ApiOperation("批量删除充电站信息")
    public String deleteStationBatch(@RequestBody int[] ids) {
        log.info("deleteStationBatch");
        for (int id : ids) {
            stationService.deleteStation(id);
        }
        return "删除成功";
    }

    @PostMapping("/createStationWithChargingPiles")
    @ApiOperation("创建充电站和对应充电桩")
    public List<ChargingPile> createStationWithChargingPiles(
            @RequestBody StationAndChargingPilesRequest request) {
        System.out.println("接收到的 Station 数据: " + request.getStation());
        System.out.println("接收到的 stationImgUrl: " + request.getStation().getStationImgUrl());

        return stationService.createStationWithChargingPiles(request.getStation(), request.getChargingPiles());
    }

    @PostMapping("/updateStationWithChargingPiles")
    @ApiOperation("更新充电站和对应充电桩")
    public ResponseEntity<?> updateStationWithChargingPiles(@RequestBody StationAndChargingPilesRequest request) {
        if (request == null) {
            log.error("Request body is null");
            throw new IllegalArgumentException("请求体不能为空");
        }
        if (request.getStation() == null) {
            log.error("Station 参数为空");
            throw new IllegalArgumentException("Station 参数不能为空");
        }
        log.info("Received Station: " + request.getStation());
        log.info("Received Charging Piles: " + request.getChargingPiles());

        return ResponseEntity.ok(stationService.updateStationWithChargingPiles(request.getStation(), request.getChargingPiles()));
    }
}
